<?php

Class Mcabang extends CI_Model{
	
	function __construct(){
		$this->load->database();
	}
	
	function create_cabang_id(){
		
		//diambil dari database berdasarkan formatnya
		$idtype = "cabang_id";

		$year   = date('y');
		$sql    = "select id from idgen where type_id = '$idtype'";
		$row    = $this->db->query($sql)->row();
		$id     = $row->id;
		//$id     += 1;
		
		return sprintf('C%04d',$id);
		/*
		C -> kode  %4d -> xxxx atau %2d -> xx 
		jadi hasilnya kalau yg %4d 0001 atay yg %2d 01
		*/
	}
	
	function simpan($cabang_id,$nama,$alamat,$kota,$kodepos,$tlp,$fax,$kepala_outlet,$hp,$date_reg,$npwp){
	
		$reg_date = date("Y-m-d H:i:s");
		$username = "ogi";
		
		$sql = "insert into cabang(cabang_id,nama,alamat,kota,kodepos,tlp,fax,kepala_outlet,hp,date_reg,npwp) 
			   values('$cabang_id','$nama','$alamat','$kota','$kodepos','$tlp','$fax','$kepala_outlet','$hp','$reg_date','$npwp')";
		//echo $sql;exit;
		$this->db->query($sql);
		
		$sql = "update idgen set id = id+1 where type_id = 'cabang_id'";
		return $this->db->query($sql);
	}
	
	function daftar_cabang(){
		$sql = "select * from cabang";
		return $this->db->query($sql)->result();
	}
}